package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class AdvimgController {
    @Autowired
    private AdvimgService advimgServier;
    @Autowired
    private AdvposService advposService;

    @GetMapping("/list")
    public ResultData list(String advimgdesc, String advimgpos, Long page, Long limit) {

        Page<Advimg> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Advimg::getAdvimgdesc, advimgdesc);
        queryWrapper.like(Advimg::getAdvimgpos, advimgpos);
        advimgServier.page(pageInfo, queryWrapper);

        Page<AdvimgDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        List<Advimg> advimgList = pageInfo.getRecords();
        if (advimgList==null){
            return ResultData.error("无数据");
        }
        List<AdvimgDto> list = advimgList.stream().map((item) -> {
            AdvimgDto advimgDto = new AdvimgDto();
            BeanUtils.copyProperties(item, advimgDto);
            String itemAdvimgpos = item.getAdvimgpos();
            if (itemAdvimgpos != null) {
                Advpos advpos = advposService.getById(itemAdvimgpos);
                advimgDto.setAdvposname(advpos.getAdvposname());
            }
            return advimgDto;
        }).collect(Collectors.toList());
        Long count = Long.valueOf(advimgServier.count());

        return ResultData.ok("",PageResult.init(count, limit, page, list));
    }

    /**
     * 添加
     * @param advimg
     * @return
     */
    @GetMapping("/add")
    public ResultData add(@RequestBody Advimg advimg) {
        advimgServier.save(advimg);
        return ResultData.ok("添加成功", null);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id) {
        LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Advimg::getId, id);
        Advimg advimg = advimgServier.getOne(queryWrapper);
        return ResultData.ok("", advimg);
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody Advimg advimg) {
        LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Advimg::getId, advimg.getId());
        Advimg advimg1 = advimgServier.getOne(queryWrapper);
        if (advimg.getAdvimgpos() != null) {
            advimg1.setAdvimgpos(advimg.getAdvimgpos());
        }
        if (advimg.getAdvimgdesc() != null) {
            advimg1.setAdvimgdesc(advimg.getAdvimgdesc());
        }
        if (advimg.getAdvimglink() != null) {
            advimg1.setAdvimglink(advimg.getAdvimglink());
        }
        if (advimg.getAdvimgsrc() != null) {
            advimg1.setAdvimgsrc(advimg.getAdvimgsrc());
        }
        advimgServier.updateById(advimg1);
        return ResultData.ok("修改成功", null);
    }

    @GetMapping("/del")
    public ResultData delete(Long id) {
        LambdaUpdateWrapper<Advimg> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Advimg::getId, id);
        queryWrapper.set(Advimg::getDefaultData, "0");
        advimgServier.update(queryWrapper);
        return ResultData.ok("删除成功", null);
    }


}
